﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ICeFramework.Win.UI
{
    public enum ICLabelType
    { 
        SingleLine,
        Multiline
    }

    public partial class ICLabel : UserControl
    {
        private string _caption;
        public string Caption
        {
            get { return _caption; }
            set 
            {
                _caption = value;
                SetLayout();
            }
        }

        private string _value;
        public string Value
        {
            get { return _value; }
            set 
            {
                _value = value;
                SetLayout();
            }
        }

        private bool _autoAlign = true;
        public bool AutoAlign
        {
            get { return _autoAlign; }
            set 
            {
                _autoAlign = value;
                SetLayout();
            }
        }

        private int _valueLeft;
        public int ValueLeft
        {
            get { return _valueLeft; }
            set
            {
                _valueLeft = value; 
                SetLayout();
            }
        }

        private int _valueWidth = 0;
        public int ValueWidth
        {
            get { return _valueWidth; }
            set
            {
                _valueWidth = value;
                SetLayout();
            }
        }

        private ICLabelType _labelType = ICLabelType.SingleLine;
        public ICLabelType LabelType
        {
            get { return _labelType; }
            set 
            {
                _labelType = value;
                SetLayout();
            }
        }

        public ICLabel()
        {
            InitializeComponent();
            SetLayout();
        }

        private void SetLayout()
        {
            this.Controls.RemoveByKey("ctrlValue");

            lblCaption.Text = _caption;
            Control ctrlValue;
            if (_labelType == ICLabelType.Multiline && Parent != null)
            {
                ctrlValue = new RichTextBox();
                RichTextBox lblValue = ctrlValue as RichTextBox;
                lblValue.ScrollBars = RichTextBoxScrollBars.Both;
                lblValue.ReadOnly = true;
                lblValue.Text = _value;
                lblValue.AutoSize = false;
                lblValue.BorderStyle = BorderStyle.Fixed3D;
                if (_valueWidth > 0)
                {
                    lblValue.Width = _valueWidth;
                }
                lblValue.Height = 100;
                lblValue.Left = 0;
                lblValue.Top = lblCaption.Top + lblCaption.Bounds.Height + 5;
            }
            else
            {
                ctrlValue = new Label();
                Label lblValue = ctrlValue as Label;
                lblValue.Text = _value;
                lblValue.AutoSize = true;
                lblValue.Top = 0;
                if (_autoAlign)
                {
                    lblValue.Left = lblCaption.Bounds.Width + 10;
                }
                else
                {
                    lblValue.Left = _valueLeft;
                }
                lblValue.BorderStyle = BorderStyle.None;
            }
            ctrlValue.Name = "ctrlValue";
            this.Controls.Add(ctrlValue);

            this.Width = ctrlValue.Left + ctrlValue.Width;
            this.Height = ctrlValue.Top + ctrlValue.Height;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            SetLayout();
        }
    }
}
